package com.foreverwin.jzymes.eap.Listener;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.service.IItemGroupAdviceService;
import com.foreverwin.jzymes.common.service.IProductionShiftService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.annotation.MessageLog;
import com.foreverwin.jzymes.production.cutter.domain.ToolBreakLog;
import com.foreverwin.jzymes.production.cutter.service.IToolBreakLogService;
import com.foreverwin.jzymes.production.tool.domain.ToolBill;
import com.foreverwin.jzymes.production.tool.domain.ToolBillItem;
import com.foreverwin.jzymes.production.tool.service.IToolBillAdviceService;
import com.foreverwin.jzymes.production.tool.service.IToolBillItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ToolBreakListener {

    @Autowired
    IProductionShiftService productionShiftService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    IToolBillAdviceService toolBillAdviceService;
    @Autowired
    IToolBillItemService toolBillItemService;
    @Autowired
    IToolBreakLogService toolBreakLogService;

    @JmsListener( destination = "MES_CNC_TOOL_CHANGE" )
    @MessageLog
    public void toolBreak( String toolBreakJson ){
        JSONObject map = JSONObject.parseObject( toolBreakJson );
        String site = map.getString( "SITE" );
        String resrce = map.getString( "RESRCE" );
        String toolLoc = map.getString( "TOOL_NUM" );
        Long maxUsage = map.getLong( "MAX_USAGE" );
        Long actualUsage = map.getLong( "ACTUAL_USAGE" );
        String breakType = map.getString( "CHANGE_TYPE" );
        Date sendTime = map.getDate( "SEND_TIME" );

        String shift = productionShiftService.findShift( site, sendTime );
        String machineType = itemGroupAdviceService.findItemGroupBySite( site );
        String cncType = resourceAdviceService.findCncType( site, resrce );
        String toolType = null;

        /**查询当前版本刀具清单*/
        ToolBill toolBill = toolBillAdviceService.selectToolBillByMachineTypeCncType( site, machineType, cncType );
        if( toolBill != null ){
            /**查询刀具清单刀号信息*/
            ToolBillItem querySet = new ToolBillItem();
            querySet.setToolBillBo( toolBill.getHandle() );
            querySet.setToolLoc( toolLoc );
            List<ToolBillItem> toolBillItems = toolBillItemService.selectToolBillItemList( querySet );
            if( toolBillItems.size() > 0 ){
                toolType = toolBillItems.get( 0 ).getToolType();
            }
        }

        /**保存换刀记录*/
        ToolBreakLog toolBreakLog = new ToolBreakLog();
        toolBreakLog.setHandle( BOHandleUtil.buildToolBreakHandle() );
        toolBreakLog.setSite( site );
        toolBreakLog.setChgTime( sendTime );
        toolBreakLog.setMachineType( machineType );
        toolBreakLog.setShift( shift );
        toolBreakLog.setResrce( resrce );
        toolBreakLog.setCncType( cncType );
        toolBreakLog.setToolType( toolType );
        toolBreakLog.setToolLoc( toolLoc );
        toolBreakLog.setQty( 1L );
        toolBreakLog.setMaxUsage( maxUsage );
        toolBreakLog.setActualUsage( actualUsage );
        toolBreakLog.setBreakType( breakType );
        toolBreakLog.setStatus( "Y" );
        toolBreakLog.setCreateType( "A" );
        toolBreakLog.setCreatedDateTime( new Date() );
        toolBreakLogService.insertToolBreakLog( toolBreakLog );

    }
}
